(Add monster's experience to Boy's and Dog's, then level them up
 and adjust their stats when necessary.  They can now gain multiple
 levels per call.)

8F/827A: AD 61 22     LDA $2261
8F/827D: 89 02 00     BIT #$0002   (is Boy's gauge absent?  probably means
                                    boy isn't in party)

8F/8280: D0 36        BNE StartDog  (if it is, skip to Dog)

8F/8282: A4 4C        LDY $4C

8F/8284: 20 55 85     JSR Init     (initialize X to point to defeated monster's
                                    stats, set boy's counter for # of levels
                                    gained from this monster to 0, and set A to 98)

8F/8287: CD 50 0A     CMP $0A50    (boy's level)
8F/828A: 90 25        BCC EndBoy   (branch if level is > 98)

8F/828C: A0 00 00     LDY #$0000
8F/828F: 20 6E 83     JSR AddExp  (add experience given by monster to
                                   Boy's current experience)

BoyLevelLoop:

8F/8292: AD 4B 0A     LDA $0A4B
8F/8295: CD 1F 4F     CMP $4F1F      (compare top two bytes of Boy's current
                                      experience to top two bytes of the total
                                      experience needed to reach the next level)

8F/8298: D0 06        BNE NoCompareBottom1  (only compare bottom two bytes of
                                             experience if the top ones matched.

                                             if they didn't, we already know whether
                                             the overall current experience is less
                                             or greater than the overall exp. needed)
                

8F/829A: AD 49 0A     LDA $0A49      (or if the top two bytes matched,)
8F/829D: CD 1D 4F     CMP $4F1D      (compare bottom two bytes of Boy's current
                                      experience to top two bytes of exp. needed
                                      for next level)

NoCompareBottom1:

8F/82A0: 90 0F        BCC EndBoy     (Carry is clear here if:
                                      1] top half of current exp < top half needed exp
                                      OR
                                      2] top half of current == top half needed, and
                                         bottom half of current < top half of needed)

8F/82A2: 22 10 83 8F  JSL $8F8310    (increase boy's level, adjust his stats
                                      accordingly, etc)
8F/82A6: 68           PLA            
8F/82A7: 1A           INC            (increase counter of # of times boy levelled
                                      from this monster)
8F/82A8: 48           PHA


8F/82A9: AD 50 0A     LDA $0A50         (Boy's level)
8F/82AC: C9 63 00     CMP #$0063  
8F/82AF: 90 E1        BCC BoyLevelLoop  (level up as many times as the boy's new
                                         experience value requires.  but don't gain any
                                         more levels if we're >= 99 already)

EndBoy:
8F/82B1: 68           PLA            (retrieve counter of # of times boy levelled
                                      from this monster)
8F/82B2: F0 04        BEQ StartDog   (don't display message if no levels gained)
8F/82B4: 22 42 83 8F  JSL PrepareLevelMessageBoy  (queue level-up message for boy)


StartDog:

8F/82B8: AD 61 4F     LDA $4F61
8F/82BB: F0 3E        BEQ Exit     (exit if dog's HP is zero)
8F/82BD: AD 61 22     LDA $2261
8F/82C0: 89 01 00     BIT #$0001   (is Dog's gauge display absent?  probably
                                    means Dog isn't in party)
8F/82C3: D0 36        BNE Exit     (if it is, exit function)

8F/82C5: A4 4C        LDY $4C
8F/82C7: 20 55 85     JSR Init

8F/82CA: CD 9A 0A     CMP $0A9A    (dog's level)
8F/82CD: 90 25        BCC EndDog   (branch if level is > 98)

8F/82CF: A0 4A 00     LDY #$004A
8F/82D2: 20 6E 83     JSR AddExp   (add experience given by monster to Dog's
                                    current experience)

DogLevelLoop:

8F/82D5: AD 95 0A     LDA $0A95
8F/82D8: CD CD 4F     CMP $4FCD      (compare top two bytes of Dog's current
                                      experience to top two bytes of the total
                                      experience needed to reach the next level)

8F/82DB: D0 06        BNE NoCompareBottom2  (only compare bottom two bytes of
                                             experience if the top ones matched.

                                             if they didn't, we already know whether
                                             the overall current experience is less
                                             or greater than the overall exp. needed)

8F/82DD: AD 93 0A     LDA $0A93      (or if the top two bytes matched,)
8F/82E0: CD CB 4F     CMP $4FCB      (compare bottom two bytes of Dog's current
                                      experience to top two bytes of exp. needed
                                      for next level)
NoCompareBottom2:

8F/82E3: 90 0F        BCC EndDog     (Carry is clear here if:
                                      1] top half of current exp < top half needed exp
                                      OR
                                      2] top half of current == top half needed, and
                                         bottom half of current < top half of needed)

                                     (NOTE: changing this instruction from a BMI to a
                                      BCC is what fixes the bug in the dog's levelling)

8F/82E5: 22 F5 84 8F  JSL $8F84F5    (increase dog's level, adjust his stats
                                      accordingly, etc)
8F/82E9: 68           PLA            
8F/82EA: 1A           INC            (increase counter of # of times dog levelled
                                      from this monster)
8F/82EB: 48           PHA


8F/82EC: AD 9A 0A     LDA $0A9A          (Dog's level)
8F/82EF: C9 63 00     CMP #$0063  
8F/82F2: 90 E1        BCC DogLevelLoop   (level up as many times as the dog's new
                                          experience value requires.  but don't gain any
                                          more levels if we're >= 99 already)

EndDog:
8F/82F4: 68           PLA            (retrieve counter of # of times dog levelled
                                      from this monster)
8F/82F5: F0 04        BEQ Exit       (don't display message if no levels gained)
8F/82F7: 22 27 85 8F  JSL PrepareLevelMessageDog   (queue level-up message for dog)

Exit:
8F/82FB: EA           NOP
8F/82FC: 6B           RTL


======================================================================================


(I separated the level-up routines from the routines to queue the level-up
 messages, and optimized for space.  Besides that, nothing's functionality
 is changed from the original.)

(Boy gains a level, and receives any accompanying stat boosts.)
(Most of the display of level-up message has been moved to a
 separate routine.)

8F/8310: AD 50 0A     LDA $0A50    (get Boy's level)
8F/8313: C9 63 00     CMP #$0063
8F/8316: B0 29        BCS Exit     (if it's >= 99, exit function)
8F/8318: 1A           INC 
8F/8319: 8D 50 0A     STA $0A50    (increment and save updated level)
8F/831C: 85 02        STA $02
8F/831E: 64 04        STZ $04      (also save the level in $00:0002 thru $00:0005,
                                    so the 80/B21F call [done via 8F/8342] can put
                                    its digits in a display buffer)
8F/8320: 22 98 83 8F  JSL $8F8398  (adjust boy's stats to account for his new
                                    level.  normally, they just grow, but because
                                    a stat's value at each level is stored
                                    separately, they could easily be hacked to
                                    decrease.)
8F/8324: AD 35 0A     LDA $0A35
8F/8327: 8D B3 4E     STA $4EB3    (current HP = max HP)
8F/832A: AD 50 0A     LDA $0A50
8F/832D: 0A           ASL 
8F/832E: 18           CLC 
8F/832F: 6D 50 0A     ADC $0A50      (use level * 3 as pointer to "Experience
                                      Needed" table, as each entry is 3 bytes)
8F/8332: AA           TAX 
8F/8333: BF D1 8B 8C  LDA $8C8BD1,X
8F/8337: 8D 1E 4F     STA $4F1E      (save total experience needed for boy
                                      to reach next level, top 2 bytes)
8F/833A: BF D0 8B 8C  LDA $8C8BD0,X
8F/833E: 8D 1D 4F     STA $4F1D      (save total experience needed for boy
                                      to reach next level, bottom byte)
Exit:
8F/8341: 6B           RTL


(Queue a "[Boy's name] reaches level [current level]" message)

PrepareLevelMessageBoy:

8F/8342: A9 22 7E     LDA #$7E22
8F/8345: 85 27        STA $27
8F/8347: A9 10 22     LDA #$2210     (will point to 7E/2210, boy's name in RAM)

8F/834A: 20 59 83     JSR CommonRoutine1

8F/834D: A9 88 83     LDA #$8388

8F/8350: 20 3E 85     JSR CommonRoutine2

8F/8353: A2 89 4E     LDX #$4E89     (point to boy's stat block?)
8F/8356: 4C 44 91     JMP $9144      (???)


(This code is used by both the boy's and dog's level-up message routine, so I
 parcelled it off into its own function to save space.)

CommonRoutine1:
8F/8359: 85 26        STA $26       (point to character's name in RAM)
8F/835B: 22 7A B3 80  JSL $80B37A   (initialize a buffer pointer in $0022 thru $0024;
                                     the Bank is 7E, and the Offset is the value
                                     of variable $0B57)
8F/835F: A5 22        LDA $22
8F/8361: 85 12        STA $12       (save pointer to the beginning of the text
                                     buffer we're about to fill and display)
8F/8363: 22 49 B1 80  JSL $80B149   (put character's name in text buffer)
8F/8367: 98           TYA 
8F/8368: 18           CLC 
8F/8369: 65 22        ADC $22
8F/836B: 85 22        STA $22       (increment buffer pointer by # of
                                     characters stored by above call)
8F/836D: 60           RTS


--------------------------


(Code used by my enlarged function 8F/827A.
 Adds experience given by monster to character's current experience.  Y indexes
 the character: 0000h points to the boy's stats, and 004Ah to the dog's.)

AddExp:

8F/836E: BF 23 00 8E  LDA $8E0023,X  (bottom two bytes of experience given
                                      by monster)
8F/8372: 18           CLC
8F/8373: 79 49 0A     ADC $0A49,Y
8F/8376: 99 49 0A     STA $0A49,Y    (add to characters's current experience)
8F/8379: BF 25 00 8E  LDA $8E0025,X  (top two bytes of experience given by
                                      monster)
8F/837D: 79 4B 0A     ADC $0A4B,Y
8F/8380: 99 4B 0A     STA $0A4B,Y    (add to character's current experience)
8F/8383: A4 4C        LDY $4C        (restore Y value)
8F/8385: 60           RTS


8F/8386: EA           NOP
8F/8387: 6B           RTL     (free bytes, but keep the RTL in case anything
                               jumps here)


------------------------


(" reaches level " text.
 holy shat!!  ASCII in a game?!  that's a novel idea..)

8F/8388: 20  (space)
8F/8389: 72  ("r")
8F/838A: 65  ("e")  
8F/838B: 61  ("a") 
8F/838C: 63  ("c")
8F/838D: 68  ("h")
8F/838E: 65  ("e")
8F/838F: 73  ("s")
8F/8390: 20  (space)
8F/8391: 6C  ("l")
8F/8392: 65  ("e")
8F/8393: 76  ("v")
8F/8394: 65  ("e")
8F/8395: 6C  ("l")
8F/8396: 20  (space)
8F/8397: 00  (null terminator)


======================================================================================


(Dog gains a level, and receives any accompanying stat boosts.)
(Most of the display of level-up message has been moved to a
 separate routine.)

8F/84F5: AD 9A 0A     LDA $0A9A     (get Dog's level)
8F/84F8: C9 63 00     CMP #$0063
8F/84FB: B0 29        BCS Exit      (if it's >= 99, exit function)
8F/84FD: 1A           INC 
8F/84FE: 8D 9A 0A     STA $0A9A     (increment and save updated level)
8F/8501: 85 02        STA $02
8F/8503: 64 04        STZ $04       (also save the level in $00:0002 thru $00:0005,
                                     so the 80/B21F call [done via 8F/8527] can put
                                     its digits in a display buffer)
8F/8505: 22 7D 85 8F  JSL $8F857D   (adjust dog's stats to account for its new
                                     level.  normally, they just grow, but because
                                     a stat's value at each level is stored
                                     separately, they could easily be hacked to
                                     decrease.)
8F/8509: AD 7F 0A     LDA $0A7F
8F/850C: 8D 61 4F     STA $4F61     (current HP = max HP)
8F/850F: AD 9A 0A     LDA $0A9A
8F/8512: 0A           ASL 
8F/8513: 18           CLC 
8F/8514: 6D 9A 0A     ADC $0A9A
8F/8517: AA           TAX            (use level * 3 as pointer to "Experience
                                      Needed" table, as each entry is 3 bytes)
8F/8518: BF FD 8C 8C  LDA $8C8CFD,X
8F/851C: 8D CC 4F     STA $4FCC      (save total experience needed for dog
                                      to reach next level, top 2 bytes)
8F/851F: BF FC 8C 8C  LDA $8C8CFC,X
8F/8523: 8D CB 4F     STA $4FCB      (save total experience needed for dog
                                      to reach next level, bottom byte)
Exit:
8F/8526: 6B           RTL


(Queue a "[Dog's name] reaches level [current level]" message)

PrepareLevelMessageDog:

8F/8527: A9 22 7E     LDA #$7E22
8F/852A: 85 27        STA $27
8F/852C: A9 34 22     LDA #$2234     (will point to 7E/2234, dog's name in RAM)

8F/852F: 20 59 83     JSR CommonRoutine1

8F/8532: A9 6D 85     LDA #$856D

8F/8535: 20 3E 85     JSR CommonRoutine2

8F/8538: A2 37 4F     LDX #$4F37     (point to dog's stat block?)
8F/853B: 4C 44 91     JMP $9144      (???)


(This code is used by both the boy's and dog's level-up message routine, so I
 parcelled it off into its own function to save space.)

CommonRoutine2:
8F/853E: 85 26        STA $26
8F/8540: A9 8F 00     LDA #$008F
8F/8543: 85 28        STA $28       ($00:0026 thru $00:0028 points to 8F/8388
                                     if boy's routine is calling, 8F/856D if
                                     dog's routine is.)
8F/8545: 20 63 83     JSR $8363     (add " reaches level " to display buffer)
8F/8548: 22 1F B2 80  JSL $80B21F   (turn a 32-bit number, stored in $00:0002
                                     thru $00:0005, into text and add it to
                                     display buffer.  in this case, it's the
                                     level number we're displaying.)
8F/854C: A5 12        LDA $12
8F/854E: 85 22        STA $22       (get pointer to beginning of text buffer)
8F/8550: 22 21 C5 8C  JSL $8CC521   (now output the whole string?
                                     "[Character name] reaches level NN")
8F/8554: 60           RTS


--------------------------


(Had to relocate part of my expanded 8F/827A)

Init:

8F/8555: BE 60 00     LDX $0060,Y  (get pointer to data of slain monster?)
8F/8558: 68           PLA          (get return address)
8F/8559: F4 00 00     PEA $0000    (Make a stack variable for how many times
                                    character levels up from this monster.
                                    Initialize to 0.)
8F/855C: 48           PHA          (now that we've gotten our variable on the
                                    stack, put the return address back.
                                    ..slimy?!  hey, i'm pressed for space.)

8F/855D: A9 62 00     LDA #$0062   (caller will compare character's level
                                    to 98 after this returns)
8F/8F60: 60           RTS


---------------------


(12 more freed bytes)

8F/8561: EA           NOP
8F/8562: EA           NOP
8F/8563: EA           NOP
8F/8564: EA           NOP
8F/8565: EA           NOP
8F/8566: EA           NOP
8F/8567: EA           NOP
8F/8568: EA           NOP
8F/8569: EA           NOP
8F/856A: EA           NOP
8F/856B: EA           NOP
8F/856C: 6B           RTL       (keep an RTL here, just in case)


-------------------------


(" reaches level " text.
 holy shat!!  ASCII in a game?!  that's a novel idea..)

(what is this still doing here when the boy has identical text after his
 level-up routine, you ask?  well, in the European versions, Square removed
 the redundancy and relocated this to be with other game text.  i generally
 like to keep the different versions of my patches -- including future ones --
 as uniform as possible, so freeing up space in just the USA ROM wouldn't've
 done me much good.)

8F/856D: 20  (space)
8F/856E: 72  ("r")
8F/856F: 65  ("e")
8F/8570: 61  ("a")
8F/8571: 63  ("c")
8F/8572: 68  ("h")
8F/8573: 65  ("e")
8F/8574: 73  ("s")
8F/8575: 20  (space)
8F/8576: 6C  ("l")
8F/8577: 65  ("e")
8F/8578: 76  ("v")
8F/8579: 65  ("e")
8F/857A: 6C  ("l")
8F/857B: 20  (space)
8F/857C: 00  (null terminator)

